package com.ryf.maven.servlet.score;

import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;
import com.ryf.maven.dao.ScoreDao;
import com.ryf.maven.dao.StudentDao;
import com.ryf.maven.entity.BaseResponse;
import com.ryf.maven.entity.Score;
import com.ryf.maven.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Description
 * @ClassName ScoreAllServlet
 * @Author L-Cohen
 * @date 2020.12.04 16:31
 */
@WebServlet(urlPatterns = "/score/all")
public class ScoreAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        BaseResponse<List<Score>> response = new BaseResponse<List<Score>>();
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        List<Score> scores = ScoreDao.getScore(StringUtils.isNullOrEmpty(page) ? 1 : Integer.parseInt(page),
                StringUtils.isNullOrEmpty(limit) ? 10 : Integer.parseInt(limit));

        int rows = StudentDao.getCount();
        response.setCode(200);
        response.setCount(rows);
        response.setMsg("请求成功");
        response.setData(scores);

        Gson gson = new Gson();
        String json = gson.toJson(response);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }
}
